package top.jandmla.excel.controller;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.jandmla.excel.bean.User;
import top.jandmla.excel.bean.UserRole;
import top.jandmla.excel.pojo.UserPojo;
import top.jandmla.excel.service.UserRoleService;
import top.jandmla.excel.service.UserService;
import top.jandmla.excel.util.result.BaseResult;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;


    @PostMapping("/getUserPojo")
    public BaseResult<UserPojo> getUserPojo(@RequestBody User user){
        if (user != null && !StringUtils.isEmpty(user.getUserName())){
            UserPojo result = userService.getPojoUser(user.getUserName());
            if (result != null){
                result.setPassword("********");
                return BaseResult.success(result);
            }else
                return BaseResult.fail(1,"查询失败！！");
        }
        return BaseResult.fail(1,"用户名为空！");
    }

    /**
     * 普通用户修改自己的个人信息
     * @param user 用户信息
     * @return 修改结果
     */
    @PostMapping("/updateUser")
    public BaseResult<String> updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }

    @PostMapping("/view")
    public BaseResult<UserPojo> userViewThemSelf(){
        UserPojo userPojo = (UserPojo) SecurityUtils.getSubject().getPrincipal();
        return userPojo != null ? BaseResult.success(userPojo) : BaseResult.fail(1,"查询失败了！");

    }

    /**
     * 辞职
     * @param userRole 身份信息
     * @return 结果
     */
    @PostMapping("/dropRole")
    public BaseResult<String> dropUserRole(@RequestBody UserRole userRole){
        return userRoleService.deleteByUser(userRole);
    }
}
